﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsForms_Logic_Questions_Generator.Operators;
using System.Text.RegularExpressions;

namespace WindowsForms_Logic_Questions_Generator.Operators
{
    class ListDataInt
    {
        private List<DataIntInterface> Question;

        public ListDataInt()
        {
            Question = new List<DataIntInterface>();
        }

        public ListDataInt(String str)
        {
            Question = new List<DataIntInterface>();
            string[] split = str.Split(' ');
            foreach (string s in split)
            {
                switch (s.Trim())
                {
                    case "":
                        break;
                    case "{+}":
                        Add(new PlusButton());
                        break;
                    case "{-}":
                        Add(new MinusButton());
                        break;
                    case "{*}":
                        Add(new MulButton());
                        break;
                    case "{/}":
                        Add(new DivButton());
                        break;
                    case "{=}":
                        Add(new EqualButton());
                        break;

                    default:
                        string pattern  = @"\{(\d+)\.\.(\d+)\}";
                        string pattern2 = @"\{(-\d+)\.\.(\d+)\}";
                        string pattern3 = @"\{(\d+)\.\.(-\d+)\}";
                        string pattern4 = @"\{(-\d+)\.\.(-\d+)\}";
                        Match match = Regex.Match(s, pattern);
                        Match match2 = Regex.Match(s, pattern2);
                        Match match3 = Regex.Match(s, pattern3);
                        Match match4 = Regex.Match(s, pattern4);
                        if ((match.Success) || (match2.Success) || (match3.Success) || (match4.Success))
                        {

                            string[] split2 = s.Split(new char[] { '{', ':', '.', '}', ' ' });
                            string from = "", to = "";

                            if (match.Success)
                            {
                                from = match.Groups[1].Value;
                                to = match.Groups[2].Value;
                            }
                            else if (match2.Success)
                            {
                                from = match2.Groups[1].Value;
                                to = match2.Groups[2].Value;
                            }
                            else if (match3.Success)
                            {
                                from = match3.Groups[1].Value;
                                to = match3.Groups[2].Value;
                            }
                            else if (match4.Success)
                            {
                                from = match4.Groups[1].Value;
                                to = match4.Groups[2].Value;
                            }
                            Add(new RangeButton(from, to));
                            break;
                        }
                        else
                        {
                            if (s.Equals("")) break;
                            Add(new FreeData(s));
                            break;
                        }
                }
            }
        }
        
        public int IndexOf (DataIntInterface obj)
        {
            return(Question.IndexOf(obj));
        }
        public void Add (DataIntInterface obj)
        {
            Question.Add(obj);
        }
        public DataIntInterface ElementAt(int index)
        {
            return Question.ElementAt(index);
        }

        public void AddTo (int index,DataIntInterface obj)
        {
            Question.Insert(index,obj);
        }
        public void Remove (DataIntInterface obj)
        {
            Question.Remove(obj);
        }
        public void Clear ()
        {
            Question.Clear();
        }
        public override string ToString()
        {
            string question = "";
            foreach (DataIntInterface obj in Question) // Loop through List with foreach
	            question += obj.ToString();
            return (question);
        }
        public DataIntInterface PrevData()
        {
            return (Question.Last());
        }
        public void FindRange(int[] arr)
        {
            int index = 0;
            foreach (DataIntInterface obj in Question)
            {

                if (obj is RangeButton)
                    arr[index] = 1;
                index++;
            }

        }
        public long HowLong()
        {
            return Question.Count();

        }

    }
    
    class ListQestions
    {
        private List<ListDataInt> Quiz;

        public ListQestions()
        {
            Quiz = new List<ListDataInt>();
        }
        public int IndexOf(ListDataInt question)
        {
            return(Quiz.IndexOf(question));
        }
        public void Add(ListDataInt question)
        {
            Quiz.Add(question);
        }
        public void To(int index, ListDataInt question)
        {
            Quiz.Insert(index, question);
        }
        public void RemoveDataInt(ListDataInt question)
        {
            Quiz.Remove(question);
        }
        public void RemoveIndex(int index)
        {
            Quiz.RemoveAt(index);
        }
        public void Clear ()
        {
            Quiz.Clear();
        }

        public ListDataInt ElementAt (int index)
        {
            return Quiz.ElementAt(index);
        }

        public void InsertAt(int index, ListDataInt question)
        {
            Quiz.Insert(index, question);
        }
        
        public string ToStringAt(int index)
        {
            return (Quiz[index].ToString());
        }

        public override string ToString()
        {
            string quiz_table = "";
            foreach (ListDataInt question in Quiz) // Loop through List with foreach
                quiz_table = quiz_table + question.ToString()+ "\n";
            return (quiz_table);
        }

        public List<ListDataInt>.Enumerator GetEnumerator()
        {
            return Quiz.GetEnumerator();
        }
        public int HowMany()
        {
            return Quiz.Count();
        }
    }
}
